package com.rgs.springboot.application;

import com.rgs.springboot.webServer.WebServer;
import org.apache.catalina.*;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import java.util.Map;

/**
 * @program: 01-custom-springboot
 * @description:
 * @author: Mr.Ren
 * @create: 2024-07-01 21:15
 **/
public class RgsApplication {

    public static void run(Class clazz) {

        //创建容器
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        //把传进来的类当作配置类，注入到容器中
        applicationContext.register(clazz);
        //刷新容器（启动容器）
        applicationContext.refresh();

//        //启动tomcat容器（需要一个容器）
//        startTomcat(applicationContext);

        //改造，项目中有tomcat就启动tomcat，有jetty就启动jetty
//        if(tomcat){
//            startTomcat
//        }else if(jetty){
//            startJetty()
//        }

        //抽象
        WebServer webServer = getWebServer(applicationContext);
        webServer.start(applicationContext);
    }

    public static WebServer getWebServer(ApplicationContext applicationContext) {
        //从容器中获取
        Map<String, WebServer> webServers = applicationContext.getBeansOfType(WebServer.class);

        if (webServers.isEmpty()) {
            throw new RuntimeException("No WebServer found");
        }
        if (webServers.size() > 1) {
            throw new RuntimeException("Multiple WebServers found");
        }
        //希望实现，引入哪个jar包就生效哪个
        return webServers.values().stream().findFirst().get();
    }

    private static void startTomcat(WebApplicationContext applicationContext) {
        Tomcat tomcat = new Tomcat();

        Server server = tomcat.getServer();
        Service service = server.findService("Tomcat");

        Connector connector = new Connector();
        connector.setPort(8082);

        Engine engine = new StandardEngine();
        engine.setDefaultHost("localhost");

        Host host = new StandardHost();
        host.setName("localhost");
        String contextPath = "";
        Context context = new StandardContext();
        context.setPath(contextPath);
        context.addLifecycleListener(new Tomcat.FixContextListener());

        host.addChild(context);
        engine.addChild(host);
        service.setContainer(engine);
        service.addConnector(connector);
        tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet(applicationContext));

        //把所有的路由映射到上面这个dispatcher，去找到对于的controller，这点属于springmvc的知识
        context.addServletMappingDecoded("/*", "dispatcher");

        try {
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }

    }
}

